package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.ManagerBean;
import com.zhuoshi.utils.DbHelper;

/**
 * 管理员登录表
 * 
 * @author Administrator
 *
 */
public class ManagerDao {
	/**
	 * 添加
	 * @param bean
	 * @return
	 */
	public int add(ManagerBean bean) {
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("insert into mt_manager ");
			sql.append("(username,password,realname,gender,role_id,role_type,telphone,email,area,province,enabled,lasttime) ");
			sql.append("values(?,?,?,?,?,?,?,?,?,?,?,?)");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getUsername());
			pstat.setString(2, bean.getPassword());
			pstat.setString(3, bean.getRealname());
			pstat.setString(4, bean.getGender());
			pstat.setInt(5, bean.getRole_id());
			pstat.setInt(6, bean.getRole_type());
			pstat.setString(7, bean.getTelphone());
			pstat.setString(8, bean.getEmail());
			pstat.setInt(9, bean.getArea());
			pstat.setInt(10, bean.getProvince());
			pstat.setInt(11, bean.getEnabled());
			pstat.setDate(12, bean.getLasttime());
			rows = pstat.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}

	//修改
	public int edit(ManagerBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("update mt_manager set ");
			sql.append("username=?,");
			sql.append("password=?,");
			sql.append("realname=?,");
			sql.append("gender=?,");
			sql.append("role_id=?,");
			sql.append("role_type=?,");
			sql.append("telphone=?,");
			sql.append("email=?,");
			sql.append("area=?,");
			sql.append("province=?,");
			sql.append("enabled=?,");
			sql.append("lasttime=?");
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getUsername());
			pstat.setString(2, bean.getPassword());
			pstat.setString(3, bean.getRealname());
			pstat.setString(4, bean.getGender());
			pstat.setInt(5, bean.getRole_id());
			pstat.setInt(6, bean.getRole_type());
			pstat.setString(7, bean.getTelphone());
			pstat.setString(8, bean.getEmail());
			pstat.setInt(9,bean.getArea());
			pstat.setInt(10, bean.getProvince());
			pstat.setInt(11, bean.getEnabled());
			pstat.setDate(12, bean.getLasttime());
			pstat.setInt(13, bean.getId());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}
	
	public int update(ManagerBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("update mt_manager set ");
			sql.append("username=?,");
			sql.append("password=?,");
			sql.append("realname=?,");
			sql.append("gender=?,");
			sql.append("role_id=?,");
			sql.append("role_type=?,");
			sql.append("telphone=?,");
			sql.append("email=?,");
			sql.append("area=?,");
			sql.append("province=?,");
			sql.append("enabled=?,");
			sql.append("lasttime=?");
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getUsername());
			pstat.setString(2, bean.getPassword());
			pstat.setString(3, bean.getRealname());
			pstat.setString(4, bean.getGender());
			pstat.setInt(5, bean.getRole_id());
			pstat.setInt(6, bean.getRole_type());
			pstat.setString(7, bean.getTelphone());
			pstat.setString(8, bean.getEmail());
			pstat.setInt(9,bean.getArea());
			pstat.setInt(10, bean.getProvince());
			pstat.setInt(11, bean.getEnabled());
			pstat.setDate(12, bean.getLasttime());
			pstat.setInt(13, bean.getId());
			rows = pstat.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int delete(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("delete from mt_manager where id=?");
			sql.append(" and id<>1");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}
	/**
	 * 通过id判断数据是否存在
	 * 
	 * @param id
	 * @return
	 */
	// static {
	// Connection conn = null;
	// PreparedStatement pstat = null;
	// ResultSet rs = null;
	// }
	public boolean exists(int id) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select count(id) from mt_manager where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			if (rs.last()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return false;
	}

	/**
	 * 通过用户名判断数据库中是否存在
	 * 
	 * @param username
	 * @return 如果数据库中有这个用户返回true 否则返回false
	 */
	public boolean exists(String username) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select count(id) from mt_manager where username=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, username);
			rs = pstat.executeQuery();
			if (rs.next()) {
				if(rs.getInt(1)>0){
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return false;
	}

	/**
	 * 登录验证
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public ManagerBean checkLogin(String username, String password) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		ManagerBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_manager where username=? and password=? and enabled=0");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, username);
			pstat.setString(2, password);
			rs = pstat.executeQuery();
			while(rs.next()) {
				bean = new ManagerBean();
				bean.setId(rs.getInt("id"));
				bean.setUsername(rs.getString("username"));
				bean.setPassword(rs.getString("password"));
				bean.setRealname(rs.getString("realname"));
				bean.setGender(rs.getString("gender"));
				bean.setRole_id(rs.getInt("role_id"));
				bean.setRole_type(rs.getInt("role_type"));
				bean.setTelphone(rs.getString("telphone"));
				bean.setEmail(rs.getString("email"));
				bean.setArea(rs.getInt("area"));
				bean.setProvince(rs.getInt("province"));
				bean.setEnabled(rs.getInt("enabled"));
				bean.setLasttime(rs.getDate("lasttime"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}

		return bean;
	}

	/**
	 * 获取管理员列表
	 * 
	 * @param bean
	 * @return
	 */
	public List<ManagerBean> getList(ManagerBean bean) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;

		List<ManagerBean> list = new ArrayList<ManagerBean>();

		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_manager where 1=1 ");
			if (bean != null) {
				if (!"".equals(bean.getUsername()) && bean.getUsername() != null) {
					sql.append(" and username like '%" + bean.getUsername() + "%'");
				}
				if (!"".equals(bean.getRealname()) && bean.getRealname() != null) {
					sql.append(" and realname like '%" + bean.getRealname() + "%'");
				}
				if (bean.getRole_id() != null) {
					sql.append(" and role_id='" + bean.getRole_id() + "'");
				}
				if (bean.getTelphone() != null) {
					sql.append(" and telphone like '%" + bean.getTelphone() + "%'");
				}
			}
			pstat = conn.prepareStatement(sql.toString());
			//System.out.println(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				ManagerBean mBean = new ManagerBean();
				mBean.setId(rs.getInt("id"));
				mBean.setUsername(rs.getString("username"));
				mBean.setPassword(rs.getString("password"));
				mBean.setRealname(rs.getString("realname"));
				mBean.setGender(rs.getString("gender"));
				mBean.setRole_id(rs.getInt("role_id"));
				mBean.setRole_type(rs.getInt("role_type"));
				mBean.setTelphone(rs.getString("telphone"));
				mBean.setEmail(rs.getString("email"));
				mBean.setArea(rs.getInt("area"));
				mBean.setProvince(rs.getInt("province"));
				mBean.setEnabled(rs.getInt("enabled"));
				mBean.setLasttime(rs.getDate("lasttime"));
				list.add(mBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}

		return list;
	}

	/**
	 * 通过id获取一个管理员实体
	 * 
	 * @param id
	 * @return
	 */
	public ManagerBean getModel(int id) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		ManagerBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_manager where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while (rs.next()) {
				bean = new ManagerBean();
				bean.setId(rs.getInt("id"));
				bean.setUsername(rs.getString("username"));
				bean.setPassword(rs.getString("password"));
				bean.setRealname(rs.getString("realname"));
				bean.setGender(rs.getString("gender"));
				bean.setRole_id(rs.getInt("role_id"));
				bean.setRole_type(rs.getInt("role_type"));
				bean.setTelphone(rs.getString("telphone"));
				bean.setEmail(rs.getString("email"));
				bean.setArea(rs.getInt("area"));
				bean.setProvince(rs.getInt("province"));
				bean.setEnabled(rs.getInt("enabled"));
				bean.setLasttime(rs.getDate("lasttime"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}

		return bean;
	}

}
